﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.LambWeaningCommands
{
    public class LambWeaningSelectHandler : IRequestHandler<LambWeaningSelectCommand, Result>
    {
        private readonly IBaseRepository<LambWeaning> lambRep;
        private readonly IBaseRepository<SleepInfor> sleepRep;
        private readonly IBaseRepository<MotherGivingBirth> motherGivingBirthRep;
        private readonly IBaseRepository<Cottages> cottagesRep;
        private readonly IBaseRepository<Fields> fieldsRep;

        public LambWeaningSelectHandler(IBaseRepository<LambWeaning> lambRep, IBaseRepository<SleepInfor> sleepRep, IBaseRepository<MotherGivingBirth> motherGivingBirthRep, IBaseRepository<Cottages> cottagesRep, IBaseRepository<Fields> fieldsRep)
        {
            this.lambRep = lambRep;
            this.sleepRep = sleepRep;
            this.motherGivingBirthRep = motherGivingBirthRep;
            this.cottagesRep = cottagesRep;
            this.fieldsRep = fieldsRep;
        }


        /// <summary>
        /// 羔羊断奶列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(LambWeaningSelectCommand request, CancellationToken cancellationToken)
        {
            var quest = new Result() { Code = (int)CodeEnum.success };

            var list = (from a in lambRep.GetAll()
                        join b in sleepRep.GetAll() on a.SleepInforId equals b.SleepInforId
                        join c in cottagesRep.GetAll() on b.RecordcottageId equals c.CottagesId
                        join d in fieldsRep.GetAll() on b.FieIdId equals d.FieIdId
                        join e in motherGivingBirthRep.GetAll() on a.LambWeaningId equals e.MotherGivingBirthId
                        join f in cottagesRep.GetAll() on a.CottagesId equals f.CottagesId
                        join g in fieldsRep.GetAll() on a.FieIdId equals g.FieIdId
                        select new LambWeaningDto()
                        {
                            LambWeaningId = a.LambWeaningId,
                            WeaningWeight = a.WeaningWeight,
                            SleepInforId = b.SleepInforId,
                            MoEarNumber = b.MoEarNumber,
                            BatchNumber = e.BatchNumber,
                            CottageName = c.CottageName,
                            FieIdId = a.FieIdId,
                            CottagesId = a.CottagesId,
                            RecordcottageId = b.RecordcottageId,
                            WeaningDate = a.WeaningDate,
                            CreateId = a.CreateId,
                            CreateTime = a.CreateTime,
                            FieIdName = d.FieIdName,
                            MotherGivingBirthId = e.MotherGivingBirthId,
                            AgeDay = (int)(DateTime.Now - Convert.ToDateTime(a.WeaningDate)).TotalDays,
                            EarNumber = b.EarNumber,
                            CottagesAfter = f.CottageName,
                            FieldAfter = g.FieIdName
                        });

            if (!string.IsNullOrEmpty(request.start))
            {
                list = list.Where(x => x.WeaningDate >= DateTime.Parse(request.start));
            }
            if (!string.IsNullOrEmpty(request.end))
            {
                list = list.Where(x => x.WeaningDate < DateTime.Parse(request.end));
            }
            if (request.sleepInforId > 0)
            {
                list = list.Where(x => x.SleepInforId == request.sleepInforId);
            }
            if (request.startday > 0)
            {
                list = list.Where(x => x.AgeDay >= request.startday);
            }
            if (request.endday > 0)
            {
                list = list.Where(x => x.AgeDay < request.endday);
            }
            if (request.weightstart > 0)
            {
                list = list.Where(x => x.WeaningWeight >= request.weightstart);
            }
            if (request.weightend > 0)
            {
                list = list.Where(x => x.WeaningWeight < request.weightend);
            }
            if (request.sleepMonth > 0)
            {
                list = list.Where(x => x.SleepInforId == request.sleepMonth);
            }

            if (list != null)
            {
                quest.Code = 1;
                quest.Data = list;
            }
           

            return quest;
        }
    }
}
